package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.GridView.GridViewDao;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.povo.NavigationPoVo;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.db.WaterListDaoImpl;

import dbhelper.XmlNameHelper;

/**
 * 图文导航一键上传
 * 
 * @author king
 * 
 */
public class NavigationUploadActivity extends Activity {
    public Button button;// 上传按钮

    public TextView textview1;// 需上传总数

    public TextView textview2;// 未上传总数

    public TextView textview3;// 进度条描述

    public TextView textview4;

    public ProgressBar pb;// 进度条
    public List<List<NavigationPoVo>> list = null;
    public boolean isuploadfile = false;
    public RequestUrl r;
    public String imsi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);

	setContentView(R.layout.waterbanding_onetoupload);
	SharedPreferenceUtil sh = new SharedPreferenceUtil(this);
	imsi = sh.getValue(XmlNameHelper.IMSI);
	r = new RequestUrl(this);
	init();
    }

    /**
     * 初始化控件、数据
     */
    public void init() {

	button = (Button) findViewById(R.id.waterbanding_upload_button);
	pb = (ProgressBar) findViewById(R.id.waterbanding_progressBar);
	textview1 = (TextView) findViewById(R.id.waterbanding_upload_total);
	textview2 = (TextView) findViewById(R.id.waterbanding_noupload_total);
	textview3 = (TextView) findViewById(R.id.waterbanding_pb_textview);
	textview4 = (TextView) findViewById(R.id.onekeyupload_info);

	textview4.setText("图文导航一键上传");
	WaterListDaoImpl dao = new WaterListDaoImpl(this);
	list = dao.queryNavigationNoUpload();
	final int noupload = list.size();
	textview1.setText(dao.queryNavigationUpload() + "");
	textview2.setText(noupload + "");
	textview3.setText("0%");

	dao.close();

	button.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (noupload <= 0) {
		    Toast.makeText(NavigationUploadActivity.this, "数据已经上传完了！", Toast.LENGTH_LONG).show();
		    return;
		}

		if (list == null | list.size() < 1) {

		    return;
		}
		pb.setMax(list.size());
		textview3.setText("正在上传中...");
		new Thread() {

		    @Override
		    public void run() {
			upload();
		    }

		}.start();

	    }
	});
    }

    // 上传
    public void upload() {
	for (int i = 0; i < list.size(); i++) {
	    List<NavigationPoVo> lists = list.get(i);
	    Map<String, String> map1 = new HashMap<String, String>();

	    Map<File, String> map3 = new HashMap<File, String>();
	    map1.put("imsi", imsi);
	    StringBuffer sb = new StringBuffer();
	    String waternumber = "";
	    for (int k = 0; k < lists.size(); k++) {
		NavigationPoVo na = lists.get(k);
		waternumber = na.getOtherid();
		if (na.getId() != null) {
		    if (na.getIsimg().equals("1")) {
			File f = new File(na.getAdddesc());
			if (f.exists()) {
			    map3.put(f, "images");
			    sb.append(f.getName()).append("<,>").append(na.getSortnumber()).append("<,>").append(na.getIsimg()).append("<;>");
			}
		    } else {
			sb.append(na.getAdddesc()).append("<,>").append(na.getSortnumber()).append("<,>").append(na.getIsimg()).append("<;>");
		    }
		}
	    }
	    map1.put("sb", sb.toString());
	    System.out.println(sb.toString() + "----------");
	    map1.put("waterNumber", waternumber);
	    String respon = UploadHttpClient.uploadSubmit(r.getUrl(RequestUrl.IMAGE_TEXT_UPLOAD), null, map3, map1);
	    if (respon.equals("error")) {
		Message msg = myHandler.obtainMessage();
		msg.what = 0;
		myHandler.sendMessage(msg);

	    } else {
		String message = respon;
		if (message.equals("yes")) {
		    Message msg = myHandler.obtainMessage();
		    msg.what = 1;
		    msg.obj = waternumber;
		    msg.arg1 = i;
		    myHandler.sendMessage(msg);
		} else {
		    Message msg = myHandler.obtainMessage();
		    msg.what = 2;
		    myHandler.sendMessage(msg);

		}
	    }
	}
    }

    Handler myHandler = new Handler() {

	@Override
	public void handleMessage(Message msg) {
	    if (msg.what == 0) {
		Toast.makeText(NavigationUploadActivity.this, "连接服务器失败", Toast.LENGTH_SHORT).show();
		textview3.setText("0%");
	    } else if (msg.what == 2) {
		textview3.setText("上传失败");
		Toast.makeText(NavigationUploadActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
	    } else if (msg.what == 1) {
		textview3.setText((float) (msg.arg1 + 1) / list.size() * 100 + "%");
		pb.setProgress((msg.arg1 + 1));
		GridViewDao dao = new GridViewDao(NavigationUploadActivity.this);
		dao.modifyUploadNavigation(msg.obj.toString(), "1");
		dao.close();
		if (msg.arg1 + 1 == list.size()) {
		    Toast.makeText(NavigationUploadActivity.this, "上传完毕", Toast.LENGTH_SHORT).show();
		    textview3.setText("100%");
		}
	    }
	    super.handleMessage(msg);
	}

    };
}
